﻿using System;
using System.Windows;
using Microsoft.Phone.BackgroundAudio;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
//using LocalSettings;
using System.IO;
using Common.Library;

namespace OnlineVideos.AudioPlaybackAgent
{
    public class AudioPlayer : AudioPlayerAgent
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        private static volatile bool _classInitialized;
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();


        public AudioPlayer()
        {
            if (!_classInitialized)
            {
                _classInitialized = true;
                // Subscribe to the managed exception handler
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    Application.Current.UnhandledException += AudioPlayer_UnhandledException;
                });
            }
        }

        /// Code to execute on Unhandled Exceptions
        private void AudioPlayer_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            //FlurryWP8SDK.Api.LogError("Error at Audio File:" + AppSettings.Title + "-" + AppSettings.AudioTitle, e.ExceptionObject.InnerException);
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
        }
        private void PlayTrack(BackgroundAudioPlayer player)
        {
            if (PlayState.Paused == player.PlayerState)
            {
                // If we're paused, we already have 
                // the track set, so just resume playing.
                player.Play();
            }
           

        }

        protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
        {
            switch (playState)
            {
                case PlayState.TrackEnded:
                    // PlayNextTrack(player);
                    break;

                case PlayState.TrackReady:
                    // The track to play is set in the PlayTrack method.
                    player.Play();
                    break;
                case PlayState.Stopped:
                    break;

            }

            NotifyComplete();
        }
        protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
        {
            switch (action)
            {
                case UserAction.Play:
                    PlayTrack(player);
                    break;

                case UserAction.Pause:
                    player.Pause();
                    break;
                case UserAction.Stop:
                    player.Stop();
                    break;

                //case UserAction.SkipPrevious:
                //    PlayPreviousTrack(player);
                //    break;

                //case UserAction.SkipNext:
                //    PlayNextTrack(player);
                //    break;
            }

            NotifyComplete();
        }

        private void PlayNextTrack(BackgroundAudioPlayer player)
        {
            throw new NotImplementedException();
        }

        private void PlayPreviousTrack(BackgroundAudioPlayer player)
        {
            throw new NotImplementedException();
        }
        protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal)
        {
            //FlurryWP8SDK.Api.LogError("Audio File "+ track.Source.AbsoluteUri.ToString()+" Not Working", error);
            if (isFatal)
            {
                Abort();
            }
            else
            {
                NotifyComplete();
            }
        }
        protected override void OnCancel()
        {

        }
    }
}
